import { defineStore } from 'pinia'
import useStorage from '@/hooks/useStorage'

const useSiteSettingsStore = defineStore({
  id: 'site-settings',
  state: () => {
    return {
      background_color: '',
      background_image: '',
      background_music: '',
      bg_mask_color: '',
      box_back_hover_color: '',
      box_background_color: '',
      box_link_align: '',
      box_link_color: '',
      box_link_hover_color: '',
      box_title_color: '',
      icon_url: '',
      other_background_image: '',
      pure_background: false,
      site_back_animation: '',
      site_desc: '',
      site_name: '',
      site_name_color: '',
      site_notice: '',
      suffix: '',
      theme: '',
      user_id: 0,
    }
  },
  actions: {
    load(val) {
      if (!val) return
      this.$patch({
        ...this.$state,
        ...val,
      })
    }
  }
})

const instance = useSiteSettingsStore()
const { getItem, setItem } = useStorage()
// set
instance.$subscribe((_, state) => {
  setItem(instance.$id, state)
})
// get
const oVal = getItem(instance.$id)
if (oVal) {
  instance.$patch({ ...oVal })
}

export default useSiteSettingsStore
